package com.creative.xcx.domain.enums;

import com.creative.constant.Constants;

import java.util.Arrays;
import java.util.List;

public enum RandomMaterial {
    IMAGE(Constants.CACHE_RANDOM_MATERIAL_IMAGE_KEY, Constants.IMAGE_SUFFIX),
    VIDEO(Constants.CACHE_RANDOM_MATERIAL_VIDEO_KEY, Constants.VIDEO_SUFFIX),
    AUDIO(Constants.CACHE_RANDOM_MATERIAL_AUDIO_KEY, Constants.AUDIO_SUFFIX);

    public final String cacheKey;
    public final String[] suffixes;

    RandomMaterial(String cacheKey, String[] suffixes) {
        this.cacheKey = cacheKey;
        this.suffixes = suffixes;
    }

    public static List<RandomMaterial> getAllTypes() {
        return Arrays.asList(values());
    }

    /**
     * 根据文件后缀匹配素材类型
     *
     * @param fileSuffix 文件后缀（如 "jpg", "mp4", "mp3"）
     * @return 对应的 RandomMaterial 类型，无匹配返回 null
     */
    public static RandomMaterial getByFileSuffix(String fileSuffix) {
        if (fileSuffix == null) return null;

        String suffix = fileSuffix.toLowerCase().replaceFirst("^\\.", "");

        for (RandomMaterial type : values()) {
            for (String allowedSuffix : type.suffixes) {
                if (suffix.equalsIgnoreCase(allowedSuffix)) {
                    return type;
                }
            }
        }
        return null;
    }
}
